package com.xyy.service.impl;

import com.xyy.entity.Feedback;
import com.xyy.mapper.FeedbackMapper;
import com.xyy.service.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 反馈服务实现类
 */
@Service
@Transactional
public class FeedbackServiceImpl implements FeedbackService {
    
    @Autowired
    private FeedbackMapper feedbackMapper;
    
    @Override
    public boolean addFeedback(Feedback feedback) {
        try {
            feedback.setCreatedAt(LocalDateTime.now());
            feedback.setUpdatedAt(LocalDateTime.now());
            if (feedback.getStatus() == null || feedback.getStatus().isEmpty()) {
                feedback.setStatus("PENDING");
            }
            return feedbackMapper.insert(feedback) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    @Override
    public Feedback getFeedbackById(Long id) {
        try {
            return feedbackMapper.selectById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    @Override
    public List<Feedback> getFeedbacksByUserId(Long userId) {
        try {
            return feedbackMapper.selectByUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    @Override
    public List<Feedback> getFeedbacksByUserIdAndStatus(Long userId, String status) {
        try {
            return feedbackMapper.selectByUserIdAndStatus(userId, status);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    @Override
    public List<Feedback> getResolvedFeedbacks(Long userId) {
        try {
            return feedbackMapper.selectByUserIdAndStatus(userId, "RESOLVED");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    @Override
    public List<Feedback> getPendingFeedbacks(Long userId) {
        try {
            return feedbackMapper.selectByUserIdAndStatus(userId, "PENDING");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    @Override
    public List<Feedback> getFeedbacksByAdminId(Long adminId) {
        try {
            return feedbackMapper.selectByAdminId(adminId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    @Override
    public List<Feedback> getAllPendingFeedbacks() {
        try {
            return feedbackMapper.selectPendingFeedbacks();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    @Override
    public boolean updateFeedbackStatus(Long id, String status, String adminReply) {
        try {
            return feedbackMapper.updateStatusAndReply(id, status, adminReply) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    @Override
    public boolean deleteFeedback(Long id) {
        try {
            return feedbackMapper.deleteById(id) > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    @Override
    public int countFeedbacksByUserId(Long userId) {
        try {
            return feedbackMapper.countByUserId(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
    
    @Override
    public int countFeedbacksByUserIdAndStatus(Long userId, String status) {
        try {
            return feedbackMapper.countByUserIdAndStatus(userId, status);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
}


